package xin.marcher.module.attribute.api.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import xin.marcher.module.attribute.api.ItemSkuAttributeApi;
import xin.marcher.module.attribute.domain.dto.ItemSkuAttributeValueResultDTO;
import xin.marcher.module.attribute.domain.dto.SkuAttributeValueDTO;
import xin.marcher.module.attribute.domain.request.ItemSkuAttributeValueRequest;
import xin.marcher.module.attribute.domain.request.QueryItemSkuAttributeValueRequest;
import xin.marcher.module.attribute.domain.request.QuerySkuIdsByAttributeRequest;
import xin.marcher.module.attribute.service.ItemSkuAttributeService;
import xin.marcher.module.common.core.JsonResult;
import xin.marcher.module.common.exception.ProductBizException;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品属性相关操作API
 */
@Slf4j
@DubboService(version = "1.0.0", interfaceClass = ItemSkuAttributeApi.class, retries = 0)
public class ItemSkuAttributeApiImpl implements ItemSkuAttributeApi {

    @Resource
    private ItemSkuAttributeService itemSkuAttributeService;

    /**
     * 保存标品和商品属性接口
     */
    @Override
    public JsonResult<ItemSkuAttributeValueResultDTO> saveItemSkuAttributeValue(List<ItemSkuAttributeValueRequest> list) {
        try {
            ItemSkuAttributeValueResultDTO itemSkuAttributeValueResultDTO = itemSkuAttributeService
                    .saveItemSkuAttributeValue(list);
            return JsonResult.buildSuccess(itemSkuAttributeValueResultDTO);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(list), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(list), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 根据商品id品类id查询聚合商品属性值接口
     */
    @Override
    public JsonResult<List<SkuAttributeValueDTO>> queryItemSkuAttributeValueList(QueryItemSkuAttributeValueRequest request) {
        try {
            List<SkuAttributeValueDTO> skuAttributeValueDTOS = itemSkuAttributeService
                    .queryItemSkuAttributeValueList(request);
            return JsonResult.buildSuccess(skuAttributeValueDTOS);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 根据属性查询条件搜索商品接口
     */
    @Override
    public JsonResult<List<String>> querySkuIdsByAttribute(QuerySkuIdsByAttributeRequest request) {
        try {
            List<String> skuIds = itemSkuAttributeService.querySkuIdsByAttributes(request);
            return JsonResult.buildSuccess(skuIds);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }
}
